package com.zy.view.diy;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.zy.utils.CanvasUtils;
import com.zy.utils.data.Point;
import com.zy.zlog.ZLog;

import java.util.List;

//堆
public class DIYCanvasView003 extends View {


    public DIYCanvasView003(Context context) {
        super(context);
        init(context);
    }


    public DIYCanvasView003(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public DIYCanvasView003(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public DIYCanvasView003(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    Paint nodePaint;
    Paint textPaint;
    Paint indexPaint;
    Paint assistLinePaint;

    private void init(Context context) {
        nodePaint = new Paint();
        nodePaint.setColor(Color.WHITE);

        textPaint = new Paint();
        //设置文本大小
        textPaint.setTextSize(72);
        textPaint.setAntiAlias(true);
        //设置文本对齐方式
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setColor(Color.RED);

        indexPaint = new Paint();
        //设置文本大小
        indexPaint.setTextSize(36);
        indexPaint.setStyle(Paint.Style.STROKE);
        indexPaint.setStrokeWidth(5);
        //设置文本对齐方式
        indexPaint.setTextAlign(Paint.Align.CENTER);
        indexPaint.setColor(Color.BLUE);


        assistLinePaint = new Paint();
        assistLinePaint.setColor(Color.GRAY);
        DashPathEffect effects = new DashPathEffect(new float[]{20, 10, 20, 10}, 10);
        assistLinePaint.setPathEffect(effects);

        pointList = CanvasUtils.buildFirst(4 * D, D, D, ss, 45, Point.Type.DEFAULT);
    }

    int D = 100;
    int PER = 45;
    private List<Point> pointList;

    int[] ss = new int[]{6, 5, 8, 7, 4, 3, 2, 1, 56, 5, 8, 7, 4, 3, 2, 1};


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        CanvasUtils.drawAssistLine(canvas, assistLinePaint, D);

        for (Point point : pointList) {
            point.draw(canvas, textPaint, indexPaint);
        }
    }

    //保存node存在的位置
    //二维数组 [0行或者 0列 不保存数据]
    //node移动后 按照就近原则复原


    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        return super.dispatchTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        ZLog.INSTANCE.d("DIYCanvasView003", "onTouchEvent:" + event);
        //return super.onTouchEvent(event);
        return true;
    }
}
